package com.itheima.sh.service;

import com.itheima.sh.dao.UserMapper;
import com.itheima.sh.pojo.User;
import com.itheima.sh.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl {
    //登录
    public User login(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User u = mapper.login(user);
        //5.释放资源
        sqlSession.close();
        //6.直接返回User对象
        return u;
    }

    //注册
    public boolean register(User user) {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //4.使用接口代理对象调用接口中的根据用户名查询的方法
            User u = mapper.queryUserByUserName(user.getUsername());
            //判断
            if (u==null) {
                mapper.register(user);
                return true;
            }else{
                return false;
            }
        } finally {
            if (sqlSession!=null) {
                sqlSession.close();
            }
        }

    }


}
